package com.ztgf.order.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.utils.ArithmeticUtils;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.order.mapper.OrderStatisticsMapper;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderStatisticsService;
import com.ztgf.order.models.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;

import static com.ztgf.common.utils.UtilConstants.*;

/**
 * author: chenyu.wang
 * date: 2020-07-21 14:39
 * Description: 订单统计服务
 */
@Slf4j
@DubboService
public class OrderStatisticsServiceImpl implements IOrderStatisticsService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public Integer queryOrderStatisticsToDay(OrderStatisticsBO bo) {
        return orderStatisticsMapper.queryOrderStatisticsToDay(bo);
    }

    @Override
    public Page<OrderVariousItemsVO> queryOrderVariousItems(OrderStatisticsBO bo) {
        Page<OrderVariousItemsVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        //查询所有最终级类目
        List<OrderVariousItemsVO> list = orderStatisticsMapper.queryAllFianlCategory(page);
        if (isNull(list)) {
            return null;
        }
        for (OrderVariousItemsVO vo : list) {
            //统计各类目订单数量
            Integer number = orderStatisticsMapper.queryOrderVariousItems(vo.getId(), bo);
            vo.setOrderNum(number);
        }
        list.sort(new Comparator<OrderVariousItemsVO>() {
            public int compare(OrderVariousItemsVO o1, OrderVariousItemsVO o2) {
                if (o1.getOrderNum() > o2.getOrderNum()) {
                    return -1;
                }
                if (o1.getOrderNum().equals(o2.getOrderNum())) {
                    return 0;
                }
                return 1;
            }
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderVariousShopItemsVO> queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderVariousShopItemsVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());

        //查询店铺类目
        List<OrderVariousShopItemsVO> list = orderStatisticsMapper.queryShopCategory(bo);
        if (isNull(list)) {
            return page;
        }
        for (OrderVariousShopItemsVO vo : list) {
            Integer number = orderStatisticsMapper.queryOrderVariousShopItems(vo.getShopId(), vo.getCategoryId(), bo);
            if (number!=null){
                vo.setOrderVolume(number);
            }else {
                vo.setOrderVolume(0);
            }

        }
        list.sort((OrderVariousShopItemsVO a,OrderVariousShopItemsVO b)->Double.valueOf(b.getOrderVolume()).compareTo(Double.valueOf(a.getOrderVolume())));
        int count=0;
        if(list != null && list.size() > 0) {
            page.setTotal(list.size());
            count = list.size();
            int fromIndex = (bo.getPageNo()-1) * bo.getPageSize();
            int toIndex = bo.getPageNo() * bo.getPageSize();
            if (toIndex > count) {
                toIndex = count;
            }
            List<OrderVariousShopItemsVO> orderSalesCategoryShopVOS = list.subList(fromIndex, toIndex);
            page.setRecords(orderSalesCategoryShopVOS);

            return page;
        }
        page.setRecords(list);

        return page;
    }

    @Override
    public OrderShopSalesToDayVO queryStoreOrders(Long shopId) {
        Date today = DateUtils.dateFormat(DateUtils.getCurrentDate(), DateUtils.DATE_FORMAT_DATEDAY);
        //今日时间
        String todays = DateUtils.dateToStr(today, DateUtils.DATE_FORMAT_DATEDAY);
        Date date = DateUtils.dateFormat(DateUtils.addDay(DateUtils.getCurrentDate(), -1), DateUtils.DATE_FORMAT_DATEDAY);
        //昨日时间
        String yestoday = DateUtils.dateToStr(date, DateUtils.DATE_FORMAT_DATEDAY);

        OrderShopSalesToDayVO vo = new OrderShopSalesToDayVO();
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(TWO);
        //今日订单量
        Integer todaySQuantity = orderStatisticsMapper.queryStoreOrders(shopId, todays,todays);
        //昨日订单量
        Integer yesterdaySQuantity = orderStatisticsMapper.queryStoreOrders(shopId, yestoday,yestoday);
        if (yesterdaySQuantity == 0 && todaySQuantity!=0) {
            vo.setOrderToYesterday(percent.format(1.00));
        } else {
            int a = todaySQuantity - yesterdaySQuantity;
            if(a==0){
                vo.setOrderToYesterday(percent.format(0.00));
            }else {
                double percentage = (float) a / (float) yesterdaySQuantity;
                vo.setOrderToYesterday(percent.format(percentage));
            }
        }
        //今日销售额
        BigDecimal todaySSales = orderStatisticsMapper.queryShopOrderSalesToDay(shopId, ONE);
        //昨日销售额
        BigDecimal yesterdaySSales = orderStatisticsMapper.queryShopOrderSalesToDay(shopId, TWO);
        if (yesterdaySSales.compareTo(BigDecimal.ZERO) == ZERO && todaySSales.compareTo(BigDecimal.ZERO) ==1) {
            vo.setSalesToYesterday(percent.format(1.00));
        }else {
            BigDecimal subtract = todaySSales.subtract(yesterdaySSales);

            if (subtract.compareTo(BigDecimal.ZERO) ==0){
                vo.setSalesToYesterday(percent.format(0.00));
            }else {
                vo.setSalesToYesterday(percent.format(subtract.divide(yesterdaySSales,TWO, BigDecimal.ROUND_HALF_UP)));
            }
        }
        vo.setOrderVolume(todaySQuantity);
        vo.setSales(todaySSales);
        return vo;
    }

    @Override
    public OrderShopAllTimeVO queryShopViews(Long shopId) {
        Date today = DateUtils.dateFormat(DateUtils.getCurrentDate(), DateUtils.DATE_FORMAT_DATEDAY);
        //今日时间
        String todays = DateUtils.dateToStr(today, DateUtils.DATE_FORMAT_DATEDAY);
        Date date = DateUtils.dateFormat(DateUtils.addDay(DateUtils.getCurrentDate(), -1), DateUtils.DATE_FORMAT_DATEDAY);
        //昨日时间
        String yestoday = DateUtils.dateToStr(date, DateUtils.DATE_FORMAT_DATEDAY);
        OrderShopAllTimeVO pageviews = new OrderShopAllTimeVO();
        //今日浏览量
        List<OrderShopTimeVO> todaySPageviews = orderStatisticsMapper.queryShopViews(shopId, ONE);
        //昨日浏览量
        List<OrderShopTimeVO> pageviewsYesterday = orderStatisticsMapper.queryShopViews(shopId, TWO);
        pageviews.setTodaySPageviews(todaySPageviews);
        pageviews.setPageviewsYesterday(pageviewsYesterday);
        return pageviews;
    }

    @Override
    public Page<OrderVariousItemsVO> queryNewCategoryOrderCount(OrderStatisticsBO bo) {
        Page<OrderVariousItemsVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderVariousItemsVO> list = orderStatisticsMapper.queryNewCategoryOrderCount(page, bo);
        page.setRecords(list);
        return page;
    }
}
